"""
Case Type   : insert
Case Name   : 账本数据库rec_num字段作为主键并发插入数据，查询数据为空
Create At   : 2024/07/12
Owner       : songjing20
Description :
    1.创建防篡改模式和用户
    2.创建表
    3.并发插入数据
    4.查询历史表数据
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.结果为0
    5.成功
History     :
"""

import os
import unittest

from yat.test import macro
from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Pubsubclass(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_node = Node(node='PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.com_pub = Common()
        self.tb_name = 't_insert_0047'
        self.user = 'u_insert_0047'
        self.schema = 's_insert_0047'

    def test_pubsub(self):
        text = '-----step1:创建防篡改模式和用户;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execut_db_sql(
            f"drop user if exists {self.user} cascade;"
            f"create user {self.user} with password '{macro.COMMON_PASSWD}';"
            f"create schema {self.schema} with blockchain;"
            f"grant ALL on SCHEMA {self.schema} to {self.user};")
        self.log.info(res)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, res,
                      f'执行失败:{text}')
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, res,
                      f'执行失败:{text}')
        self.assertIn('GRANT', res, f'执行失败:{text}')

        text = '-----step2:创建表;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execut_db_sql(
            f"drop table if exists {self.schema}.{self.tb_name} cascade;"
            f"create table {self.schema}.{self.tb_name}(c1 int,c2 money);")
        self.log.info(res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res,
                      f'建表失败:{text}')

        text = '-----step3:并发插入数据;expect:成功-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH};
        for i in $(seq 1 20);do
            gsql -d {self.pri_node.db_name} -p {self.pri_node.db_port} \
            -c"insert into {self.schema}.{self.tb_name} values(\
            generate_series(1,500),$i);" &
        done;'''
        self.log.info(cmd)
        for i in range(3):
            res = self.pri_node.sh(cmd).result()
            self.log.info(res)
            self.assertEqual(res.count(self.constant.INSERT_SUCCESS_MSG), 20,
                             f'执行失败:{text}')

        text = '-----step4:查询历史表;expect:结果为0-----'
        self.log.info(text)
        select_res = self.pri_sh.execut_db_sql(
            f"select count(*)-count(distinct rec_num) "
            f"from blockchain.{self.schema}_{self.tb_name}_hist;")
        self.log.info(select_res)
        self.assertIn(' 0\n(1 row)', select_res, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step5:清理环境;expect:成功-----'
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop schema if exists {self.schema} cascade;"
            f"drop user {self.user} cascade")
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, drop_res,
                      f'删表失败:{text}')
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, drop_res,
                      f'删表失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
